#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;
const double alpha = 0.7;

int n, root;
struct node
{
    int ls, rs, val, tot, size, del;
}t[N];
int order[N], cnt;
int avai[N], top;

void dfs(int x)
{
    if (x == 0) return;
    dfs(t[x].ls);
    if (t[x].del) order[++cnt] = x;
    else avai[++top] = x;
    dfs(t[x].rs);
}

void initnode(int x)
{
    t[x].ls = t[x].rs = 0;
    t[x].tot = t[x].size = t[x].del = 1;
}

void build(int l, int r, int& x)
{
    int mid = (l + r) >> 1;
    x = order[mid];
    if (l == r) { initnode(x); return; }
    if (l < mid) build(l, mid - 1, t[x].ls);
    if (l == mid) t[x].ls = 0;
    build(mid + 1, r, t[x].rs);
    t[x].size = t[t[x].ls].size + t[t[x].rs].size + 1;
    t[x].tot = t[t[x].ls].tot + t[t[x].rs].tot + 1;
}

void rebuild(int& x)
{
    cnt = 0; dfs(x);
    if (cnt) build(1, cnt, x);
    else x = 0;
}

bool balance(int x)
{
    if ((double)max(t[t[x].ls].size, t[t[x].rs].size) >= (double)t[x].size * alpha) return 1;
    return 0;
}

void Insert(int& x, int v)
{
    if (x == 0) {
        x = avai[top--];
        t[x].val = v;
        initnode(x);
        return;
    }
    t[x].size++;t[x].tot++;
    if (v <= t[x].val) Insert(t[x].ls, v);
    else Insert(t[x].rs, v);
    if (balance(x)) rebuild(x);
}

int Rank(int x, int v)
{
    if (x == 0) return 0;
    if (v > t[x].val) return t[t[x].ls].size + t[x].del + Rank(t[x].rs, v);
    return Rank(t[x].ls, v);
}

int kth(int k)
{
    int x = root;
    while (x) {
        if (t[x].del && t[t[x].ls].size + 1 == k) return t[x].val;
        else if (t[t[x].ls].size >= k) x = t[x].ls;
        else {
            k -= t[t[x].ls].size + t[x].del;
            x = t[x].rs;
        }
    }
    return t[x].val;
}

void Del_k(int& x, int k)
{
    t[x].size--;
    if (t[x].del && t[t[x].ls].size + 1 == k) {
        t[x].del = 0;
        return;
    }
    if (t[t[x].ls].size + t[x].del >= k) Del_k(t[x].ls, k);
    else Del_k(t[x].rs, k - t[t[x].ls].size - t[x].del);
}

void Del(int x)
{
    Del_k(root, Rank(root, x) + 1);
    if (t[root].tot * alpha >= t[root].size)
        rebuild(root);
}

void solve()
{
    for (int i = N - 1;i >= 1;i--) avai[++top] = i;
    int q;cin >> q;
    while (q--) {
        int op, x; cin >> op >> x;
        if (op == 1) Insert(root, x);
        if (op == 2) Del(x);
        if (op == 3) cout << Rank(root, x) + 1 << '\n';
        if (op == 4) cout << kth(x) << '\n';
        if (op == 5) cout << kth(Rank(root, x)) << '\n';
        if (op == 6) cout << kth(Rank(root, x + 1) + 1) << '\n';
    }
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}